﻿#set platform related cmake definition and config
message("Load Platform Android CMake")

if( ${DEBUG_PROJECT_INTERNAL} )
    set(NS_LIBS_ROOT ${ENGINE_ROOT_PATH}/Build/AndroidStudio/NSEngine/NSEngineLib/build/intermediates/cmake/debug/obj/arm64-v8a)
	set(NS_TOOLS_ROOT ${ENGINE_ROOT_PATH}/../out/Release/NSEngineLib/tools)
    message("DEBUG ENGINE LIB PATH: " ${NS_LIBS_ROOT})
	message("DEBUG ENGINE TOOLS PATH: " ${NS_TOOLS_ROOT})
else()
    set(NS_LIBS_ROOT ${ENGINE_ROOT_PATH}/android/${TARGET_ARCH})
	set(NS_TOOLS_ROOT ${ENGINE_ROOT_PATH}/tools)
endif()

set(PROJECT_LIBS_ROOT ${DIR_PROJECT_LIB}/android/${TARGET_ARCH})

if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    message("[D] BUILD TYPE IS " ${CMAKE_BUILD_TYPE})
else()
    message("[R] BUILD TYPE IS " ${CMAKE_BUILD_TYPE})
    # -s选项为strip，不strip生成的库文件会很大
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s -Wl")
endif()

if( EXISTS "${PROJECT_SOURCE_DIR}/CMake/IDE/PlatformAndroidIDE.cmake" )
    include(${PROJECT_SOURCE_DIR}/CMake/IDE/PlatformAndroidIDE.cmake)
endif()

if( EXISTS "${PROJECT_SOURCE_DIR}/CMake/PlatformAndroidCustom.cmake" )
    include(${PROJECT_SOURCE_DIR}/CMake/PlatformAndroidCustom.cmake)
endif()

set(PLATFORM_SOURCE 
        #Platform/Android
		${PROJECT_PLATFORM_SOURCE}
        ${PROJECT_PLATFORM_CUSTOM_SOURCE}
)

add_library(libNSEngine SHARED IMPORTED)
set_target_properties(libNSEngine PROPERTIES IMPORTED_LOCATION ${NS_LIBS_ROOT}/libNSEngine.so)
set(ENGINE_LIB_NAME libNSEngine)


if(ENGINE_DEBUG)
#Physx4.1使用DEBUG库需要声明-DNDEBUG
#定义后每个GL CALL会进行ERROR检查
    list(INSERT PLATFORM_DEFINITIONS 0 "DEBUG_OPENGL" "NDEBUG")
endif(ENGINE_DEBUG)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)


# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
INCLUDE_DIRECTORIES(${ANDROID_NDK}/sources/android/cpufeatures)
#add_library(cpu-features
#        STATIC
#        ${ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c)
#add_library(app-glue
#        STATIC
#        ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)

list(APPEND PLATFORM_LINK_LIBS 
    ${log-lib}
    GLESv3
    EGL
    m
    android
    jnigraphics
   ${ENGINE_LIB_NAME}
)

if( EXISTS "${PROJECT_SOURCE_DIR}/CMake/IDE/PlatformAndroidDepIDE.cmake" )
	include(${PROJECT_SOURCE_DIR}/CMake/IDE/PlatformAndroidDepIDE.cmake)
endif()

message(">>>>>>>>> Platform Android link libs: " ${PLATFORM_LINK_LIBS})
message(">>>>>>>>> Engine Android link libs: " ${ENGINE_LINK_LIBS})

#NOTE: Android CMake CXX FLAGS and LINK FLAGS all specified in build.gradle. The following flags will be override by build.gradle cppFlags
#USELESS IN ANDROID BUILD
set(PLATFORM_CXX_FLAGS "${PROJECT_PLATFORM_CXX_FLAGS} ${PROJECT_PLATFORM_CUSTOM_CXX_FLAGS}")

#USELESS IN ANDROID BUILD
set(PLATFORM_LINK_FLAGS "${PROJECT_PLATFORM_LINK_FLAGS} ${PROJECT_PLATFORM_CUSTOM_LINK_FLAGS}")

message(">>>>>>>>> Platform Android CXX flags: " ${PLATFORM_CXX_FLAGS})
message(">>>>>>>>> Platform Android LINK flags: " ${PLATFORM_LINK_FLAGS})
